import {
  ResultMessage,
  BuildTaskParam,
  ListTaskParam,
  BuildTaskRecord,
  BuildTaskResult,
} from "../model";
import { axiosPost } from "./http";

export const addTask = async (param: BuildTaskParam) => {
  const rslt = await axiosPost<ResultMessage<string>>("/addTask", param);
  if (rslt.code !== 200) throw new Error(rslt.data);
  return rslt;
};

export const listTask = async (param: ListTaskParam) => {
  const rslt = await axiosPost<ResultMessage<any>>("/listTask", param);
  if (rslt.code !== 200) throw new Error(rslt.data as string);
  return rslt.data as Pick<
    BuildTaskRecord,
    "taskId" | "taskTitle" | "taskDesc" | "status"
  >[];
};

export const getTaskDetail = async (taskId: string) => {
  const rslt = await axiosPost<ResultMessage<BuildTaskRecord | null | string>>(
    "/taskDetail",
    { taskId }
  );
  if (rslt.code !== 200) throw new Error(rslt.data as string);
  return rslt.data as BuildTaskRecord | null;
};

export const listTaskResultMap = async (taskId: string) => {
  const rslt = await axiosPost<
    ResultMessage<Record<string, BuildTaskResult[]> | string>
  >("/listTaskResultMap", { taskId });
  if (rslt.code !== 200) throw new Error(rslt.data as string);
  return rslt.data as Record<string, BuildTaskResult[]>;
};

export const clearByTaskId = async (taskId: string) => {
  const rslt = await axiosPost<ResultMessage<string>>("/clearByTaskId", {
    taskId,
  });
  if (rslt.code !== 200) throw new Error(rslt.data as string);
  return rslt.data;
};

export const clearAllTask = async () => {
  const rslt = await axiosPost<ResultMessage<string>>("/clearAll");
  if (rslt.code !== 200) throw new Error(rslt.data as string);
  return rslt.data;
};
